<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    :width="'40%'"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="字典id" prop="dictId">
        <el-input v-model="dataForm.dictId" placeholder="字典id"></el-input>
      </el-form-item> -->
      <el-form-item label="文本" prop="itemText">
        <el-input v-model="dataForm.itemText" placeholder="文本"></el-input>
      </el-form-item>
      <el-form-item label="项值" prop="itemValue">
        <el-input v-model="dataForm.itemValue" placeholder="值"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          type="textarea"
          v-model="dataForm.description"
          placeholder="描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        dictId: "",
        itemText: "",
        itemValue: "",
        description: "",
        sort: "",
        createDate: "",
      },
      dataRule: {
        itemText: [{ required: true, message: "字典项文本不能为空", trigger: "blur" }],
        itemValue: [{ required: true, message: "字典项值不能为空", trigger: "blur" }],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id, dictId) {
      this.dataForm.id = id || 0;
      this.dataForm.dictId = dictId || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/app/dictitem/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.dictId = data.sysDictItem.dictId;
              this.dataForm.itemText = data.sysDictItem.itemText;
              this.dataForm.itemValue = data.sysDictItem.itemValue;
              this.dataForm.description = data.sysDictItem.description;
              this.dataForm.sort = data.sysDictItem.sort;
              this.dataForm.createDate = data.sysDictItem.createDate;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/app/dictitem/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              dictId: this.dataForm.dictId,
              itemText: this.dataForm.itemText,
              itemValue: this.dataForm.itemValue,
              description: this.dataForm.description,
              sort: this.dataForm.sort,
              createDate: this.dataForm.createDate,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
